﻿using SFML.Graphics;
using SFML.System;

namespace WolfCarol.SFU
{
    public class Camera
    {
        private static Camera? _main;

        public static Camera Main
        {
            get
            {
                if (_main == null)
                {
                    throw new NullReferenceException("Instance of Camera was null.");
                }

                return _main;
            }
            private set
            {
                _main = value;
            }
        }

        private View _view;

        public Vector2f Position
        {
            get => _view.Center;
            set => _view.Center = value;
        }

        public float Rotation
        {
            get => _view.Rotation;
            set => _view.Rotation = value;
        }

        public Camera()
        {
            _view = new View(new FloatRect(0f, 0f, Window.Instance.Width, Window.Instance.Height));
            Window.Instance.AddView(_view);
            _main = this;
        }

        public void SetDisplayArea(FloatRect rect)
        {
            _view.Viewport = rect;
            _view.Size = new Vector2f(Window.Instance.Width * rect.Width, Window.Instance.Height * rect.Height);
        }

        public Vector2f ScreenToWorldPoint(Vector2i screenPosition)
        {
            return Window.Instance.MapPixelToCoords(screenPosition, _view);
        }

        public Vector2i WorldToScreenPoint(Vector2f worldPosition)
        {
            return Window.Instance.MapCoordsToPixel(worldPosition, _view);
        }
    }
}
